package com.javatip;

//import com.javatip.project.Change.predict.service.IChangePredictService;
//import com.javatip.project.Change.trainMission.service.IChangeTrainMissionService;
import com.javatip.project.deep_learning.predict.service.IPredictService;
import com.javatip.project.deep_learning.trainMission.service.ITrainMissionService;
import com.javatip.project.queue.domain.Task;
import com.javatip.project.queue.service.QueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Objects;

/**
 * 启动程序
 * 
 * @author ruoyi
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableScheduling
public class       JavatipApplication implements CommandLineRunner {
    @Autowired
    private QueueService queueService;
    @Autowired
    private ITrainMissionService trainMissionService;
    @Autowired
    private IPredictService predictService;
//    @Autowired
//    private IChangeTrainMissionService changeTrainMissionService;
//    @Autowired
//    private IChangePredictService changePredictService;

    public static void main(String[] args) {
//        RunPythonScript.runCommand("D:/Develop/Java/xx-system/src/main/resources/templates/test.py");
//        DoTrain doTrain = new DoTrain();
//        doTrain.doTrain();

        SpringApplication.run(JavatipApplication.class, args);

    }

    @Override
    public void run(String... args) throws Exception {
        //找出服务器重启中断的任务，启动训练
        queueService.getAllTasksInOrder().forEach(task -> {


            int taskID=task.getTaskID();
            String taskType=task.getTaskType();
            //查看任务的状态,如果是训练中,就开始训练
            if (Objects.equals(taskType, "segTrain")) {


                if (Objects.equals(trainMissionService.selectTrainMissionById(taskID).getStatus(), "训练中")) {

                    trainMissionService.startSingleTraining(taskID );
                }

            }

            else if  (Objects.equals(taskType, "segPredict")) {


                if  (Objects.equals(predictService.selectPredictById(taskID).getStatus(), "训练中")){
                    predictService.predict(taskID);
                }

            }
//            if  (Objects.equals(taskType, "cdTrain")) {
//                if (Objects.equals(changeTrainMissionService.selectChangeTrainMissionById(taskID).getStatus(), "训练中"))  {
//                    changeTrainMissionService.startSingleTraining(taskID );
//                }
//
//            }
//            if  (Objects.equals(taskType, "cdPredict")) {
//                if (Objects.equals(changePredictService.selectPredictById(taskID).getStatus(), "训练中")) {
//                    changePredictService.predict(taskID);
//                }
//
//            }



        });



    }
}